package Ep04_String.LC_0003;

import java.util.HashMap;

/**
 * @author Jimmy Zhan WORKSTATION
 * @since 2023/7/11 16:31
 * 说明：
 */

public class Best {
    public static void main(String[] args) {
        Best m = new Best();
        System.out.println(m.lengthOfLongestSubstring("dvdf"));
    }
    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) return 0;
        if (s.length() == 1) return 1;
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0;
        int left = 0;
        for(int i = 0; i < s.length(); ++i){
            if(map.containsKey(s.charAt(i))){
                // 遇到了重复的，就从重复处向前滑动一格
                left = Math.max(left,map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;
    }
}

